package com.wise.wing.views;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bm.corelibs.utils.ViewHolder;
import com.wise.wing.R;


/**
 * 下拉选择
 * @Copyright Copyright © 2014 蓝色互动. All rights reserved.
 *
 * @author ZhuFan
 * @Date: 2015-3-26
 */
@SuppressWarnings("rawtypes")
public class PopMenu implements android.widget.AdapterView.OnItemClickListener {
	
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 数据列表
	 */
	private ListView listView;
	/**
	 * 数据源
	 */
	private List data = new ArrayList();
	/**
	 * 弹出窗
	 */
	private PopupWindow menu;
	/**
	 * 父窗体
	 */
	private TextView parent;
	/**
	 * 选择的下标
	 */
	private int selectedItem = 0;
	/**
	 * 适配器
	 */
	private MenuAdapter adapter;
	private LinearLayout ll_parent;
	private OnMenuItemClickedListener listener;
	
	public PopMenu(Context context, TextView parent){
		this.context = context;
		this.parent = parent;
		init();
	}
	
	public PopMenu(Context context, TextView parent, int selectedItem){
		this.context = context;
		this.parent = parent;
		this.selectedItem = selectedItem;
		init();
	}
	
	public PopMenu(Context context, TextView parent, List data){
		this.context = context;
		this.parent = parent;
		this.data = data;
		init();
	}
	
	public PopMenu(Context context, TextView parent, List data, int selectedItem){
		this.context = context;
		this.parent = parent;
		this.data = data;
		this.selectedItem = selectedItem;
		init();
	}
	
	public void setOnMenuItemClickedListener(OnMenuItemClickedListener l) {
		listener = l;
	}
	
	/**
	 * 获取数据源
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @return 
	 * @return List<String>
	 * @throws 
	 * @Date 2015-3-26 下午3:20:52
	 */
	public List getData() {
		return data;
	}
	
	/**
	 * 设置数据源
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @param data 
	 * @return void
	 * @throws 
	 * @Date 2015-3-26 下午3:20:57
	 */
	public void setData(List data) {
		this.data = data;
		adapter.notifyDataSetChanged();
	}
	
	/**
	 * 获取选中的条目下标
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @return 
	 * @return int
	 * @throws 
	 * @Date 2015-3-26 下午3:21:03
	 */
	public int getSelectedItem() {
		return selectedItem;
	}
	
	/**
	 * 设置选中的条目下标
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @param selectedItem 
	 * @return void
	 * @throws 
	 * @Date 2015-3-26 下午3:21:23
	 */
	public void setSelectedItem(int selectedItem) {
		this.selectedItem = selectedItem;
		adapter.notifyDataSetChanged();
		this.parent.setText(data.get(selectedItem).toString());
	}
	
	/**
	 * 初始化
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views 
	 * @return void
	 * @throws 
	 * @Date 2015-3-26 下午3:21:46
	 */
	private void init(){
		initMenu();
	}
	
	/**
	 * 初始化弹出窗
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views 
	 * @return void
	 * @throws 
	 * @Date 2015-3-26 下午3:22:02
	 */
	@SuppressLint("InflateParams") @SuppressWarnings("deprecation")
	private void initMenu(){
		View view = LayoutInflater.from(context).inflate(R.layout.menu_search, null);
		menu = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		menu.setFocusable(true);
		menu.setOutsideTouchable(true);
		menu.setBackgroundDrawable(new BitmapDrawable());
		menu.update();
		
		initList(view);
	}
	
	/**
	 * 初始化数据列表
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @param view 
	 * @return void
	 * @throws 
	 * @Date 2015-3-26 下午3:22:12
	 */
	private void initList(View view){
		ll_parent = (LinearLayout) view.findViewById(R.id.ll_parent);
		int width = parent.getWidth();
		LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) ll_parent.getLayoutParams();
		layoutParams.width = width;
		
		ll_parent.setLayoutParams(layoutParams);
		
		listView = (ListView) view.findViewById(R.id.lv_menu);
		adapter = new MenuAdapter();
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
	}
	
	/**
	 * 适配器
	 * @Copyright Copyright © 2014 蓝色互动. All rights reserved.
	 *
	 * @author ZhuFan
	 * @Date: 2015-3-26
	 */
	class MenuAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressLint("InflateParams") @Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null)
				convertView = LayoutInflater.from(context).inflate(R.layout.item_search_menu, null);
			
			TextView text = ViewHolder.get(convertView, R.id.tv_item_menu);
			text.setText(data.get(position).toString());
			
//			if(position == selectedItem)
//				text.setTextColor(context.getResources().getColor(R.color.main));
//			else
				text.setTextColor(context.getResources().getColor(R.color.main_text));
			
			return convertView;
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		setSelectedItem(position);
		if(listener != null)
			listener.onMenuItemClicked(position, data.get(position));
		menu.dismiss();
	}
	
	/**
	 * 显示下拉菜单
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views 
	 * @return void
	 * @throws 
	 * @Date 2015-3-26 下午3:22:30
	 */
	public void show(){
		if(!menu.isShowing()) {
			menu.showAsDropDown(parent);
		}
	}
	
	/**
	 * 取消下拉菜单
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views 
	 * @return void
	 * @throws 
	 * @Date 2015-3-26 下午3:22:39
	 */
	public void dismiss() {
		if(menu.isShowing()) {
			menu.dismiss();
		}
	}
	
	public interface OnMenuItemClickedListener {
		void onMenuItemClicked(int position, Object data);
	}
}
